﻿<template>
	<div class="boardDisplayDesign-container">
		<el-dialog v-model="isShowDialog"  :width="1600" draggable="">
			<template #header>
				<div style="color: #fff">
					<!--<el-icon size="16" style="margin-right: 3px; display: inline; vertical-align: middle"> <ele-Edit /> </el-icon>-->
					<span>{{ props.title }}</span>
				</div>
			</template>
			<el-form :model="ruleForm" ref="ruleFormRef" label-width="auto" :rules="rules">
				<el-row :gutter="35">
					<el-form-item v-show="false">
						<el-input v-model="ruleForm.id" />
					</el-form-item>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="设计编号" prop="designNo">
							<el-input v-model="ruleForm.designNo" placeholder="请输入设计编号" maxlength="32" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="组态名称" prop="name">
							<el-input v-model="ruleForm.name" placeholder="请输入组态名称" maxlength="100" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="路由清单" prop="routeNos">
							<el-input v-model="ruleForm.routeNos" placeholder="请输入路由清单" maxlength="500" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="路径" prop="path">
							<el-input v-model="ruleForm.path" placeholder="请输入路径" maxlength="32" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
				</el-row>
			</el-form>

			<el-tabs v-loading="state.loading" v-model="state.selectedTabName">
				<el-tab-pane label="路径信息">
					<el-form :model="state.DisplayForm" ref="DisplayForm" abel-width="auto" >
						<el-row :gutter="35">
							<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
								<el-button icon="ele-Plus" type="primary" plain @click="addMaterialRow"> 增加 </el-button>
								<span style="font-size: 12px; color: gray; padding-left: 5px">  </span>
					</el-col>
										<el-col :xs="24" :sm="3" :md="3" :lg="3" :xl="3" class="mb20">
											<el-text>类型</el-text>
										</el-col> 
										<el-col :xs="24" :sm="2" :md="2" :lg="2" :xl="2" class="mb20" >
											<el-text>方向</el-text>
										</el-col> 
										<el-col :xs="24" :sm="2" :md="2" :lg="2" :xl="2" class="mb20">
											<el-text>SBID</el-text>
										</el-col> 
										<el-col :xs="24" :sm="2" :md="2" :lg="2" :xl="2" class="mb20">
											<el-text>Left</el-text>
										</el-col> 
										<el-col :xs="24" :sm="2" :md="2" :lg="2" :xl="2" class="mb20">
											<el-text>Top</el-text>
										</el-col> 
										<el-col :xs="24" :sm="2" :md="2" :lg="2" :xl="2" class="mb20">
											<el-text>endLeft</el-text>
										</el-col> 
										<el-col :xs="24" :sm="2" :md="2" :lg="2" :xl="2" class="mb20">
											<el-text>endTop</el-text>
										</el-col> 
										<el-col :xs="24" :sm="2" :md="2" :lg="2" :xl="2" class="mb20">
											<el-text>width</el-text>
										</el-col> 
										<el-col :xs="24" :sm="2" :md="2" :lg="2" :xl="2" class="mb20">
											<el-text>height</el-text>
										</el-col> 
										<el-col :xs="24" :sm="2" :md="2" :lg="2" :xl="2" class="mb20">
											<el-text>radius</el-text>
										</el-col> 
										<el-col :xs="24" :sm="1" :md="1" :lg="1" :xl="1" class="mb20" >
											<el-text>color</el-text>
										</el-col> 
										<el-col :xs="24" :sm="1" :md="1" :lg="1" :xl="1" class="mb20">
											<el-text>arrow</el-text>
										</el-col>
										<el-col :xs="24" :sm="1" :md="1" :lg="1" :xl="1" class="mb20">
											<el-text></el-text>
									</el-col>
									</el-row>


						<el-row>
							
							<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
								<template v-if="state.DisplayForm.Displayitems != undefined && state.DisplayForm.Displayitems.length > 0">
									<el-row :gutter="35" v-for="(v, k) in state.DisplayForm.Displayitems" :key="k">
										<el-col :xs="24" :sm="3" :md="3" :lg="3" :xl="3" class="mb20">
											<el-form-item :prop="`Displayitems[${k}].itemType`" >

												<el-select clearable v-model="state.DisplayForm.Displayitems[k].itemType" placeholder="请选择类型">
										<el-option v-for="(item,index) in dl('BoardItemType')"  :key="index" :value="item.code" :label="`[${item.code}] ${item.value}`"></el-option>
										
									</el-select>
											</el-form-item>
										</el-col>
										<el-col :xs="24" :sm="2" :md="2" :lg="2" :xl="2" class="mb20" >
											<el-form-item  :prop="`Displayitems[${k}].direction`" >

												<el-select clearable v-model="state.DisplayForm.Displayitems[k].direction" placeholder="请选择方向">
													<el-option v-for="(item,index) in dl('SwichDirection')"  :key="index" :value="item.code" :label="`[${item.code}] ${item.value}`"></el-option>										
												</el-select>
											</el-form-item>
										</el-col>
										<el-col :xs="24" :sm="2" :md="2" :lg="2" :xl="2" class="mb20">
										<el-form-item :prop="`Displayitems[${k}].sid`" :rules="[{ required: true, message: `请输入 SBID`, trigger: 'blur' }]">
											<el-input placeholder="SBID"  v-model="state.DisplayForm.Displayitems[k].sid" clearable />
										</el-form-item>
										
									</el-col>
										<el-col :xs="24" :sm="2" :md="2" :lg="2" :xl="2" class="mb20">
										<el-form-item :prop="`Displayitems[${k}].left`" :rules="[{ required: true, message: `请输入 Left`, trigger: 'blur' }]">
											<el-input placeholder="Left"  v-model="state.DisplayForm.Displayitems[k].left" clearable />
										</el-form-item>
										
									</el-col>
									<el-col :xs="24" :sm="2" :md="2" :lg="2" :xl="2" class="mb20">
										<el-form-item  :prop="`Displayitems[${k}].top`" :rules="[{ required: true, message: `请输入 Top`, trigger: 'blur' }]">
											<el-input v-model="state.DisplayForm.Displayitems[k].top" placeholder="top" clearable />
										</el-form-item>
									</el-col>
									<el-col :xs="24" :sm="2" :md="2" :lg="2" :xl="2" class="mb20">
										<el-form-item  :prop="`Displayitems[${k}].endLeft`">
											<el-input placeholder="endLeft"  v-model="state.DisplayForm.Displayitems[k].endLeft" clearable />
										</el-form-item>
									</el-col>
									<el-col :xs="24" :sm="2" :md="2" :lg="2" :xl="2" class="mb20">
										<el-form-item  :prop="`Displayitems[${k}].endTop`" >
											<el-input v-model="state.DisplayForm.Displayitems[k].endTop" placeholder="endTop" clearable />
										</el-form-item>
									</el-col>
									<el-col :xs="24" :sm="2" :md="2" :lg="2" :xl="2" class="mb20">
										<el-form-item  :prop="`Displayitems[${k}].width`">
											<el-input placeholder="width"  v-model="state.DisplayForm.Displayitems[k].width" clearable />
										</el-form-item>
									</el-col>
									<el-col :xs="24" :sm="2" :md="2" :lg="2" :xl="2" class="mb20">
										<el-form-item  :prop="`Displayitems[${k}].height`" >
											<el-input v-model="state.DisplayForm.Displayitems[k].height" placeholder="height" clearable />
										</el-form-item>
									</el-col>
									<el-col :xs="24" :sm="2" :md="2" :lg="2" :xl="2" class="mb20">
										<el-form-item  :prop="`Displayitems[${k}].radius`" >
											<el-input v-model="state.DisplayForm.Displayitems[k].radius" placeholder="radius" clearable />
										</el-form-item>
									</el-col>
									<el-col :xs="24" :sm="1" :md="1" :lg="1" :xl="1" class="mb20" >
										<el-form-item :prop="`Displayitems[${k}].color`" >
											<el-color-picker v-model="state.DisplayForm.Displayitems[k].color" :predefine="predefineColors"/>
										</el-form-item>
									</el-col>
									<el-col :xs="24" :sm="1" :md="1" :lg="1" :xl="1" class="mb20" >
										<el-form-item :prop="`Displayitems[${k}].arrow`" >
											<!-- <el-input v-model="state.DisplayForm.Displayitems[k].arrow" placeholder="arrow" clearable /> -->
											<el-switch size="small"
											v-model="state.DisplayForm.Displayitems[k].arrow"
												style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
												:active-value="true"
												:inactive-value="false"
												/>
										</el-form-item>
									</el-col>
									<el-col :xs="24" :sm="1" :md="1" :lg="1" :xl="1" class="mb20">
										<el-button icon="ele-Delete" type="danger" circle plain size="small" @click="deleteMaterialRow(k)" />
									</el-col>
									</el-row>
								</template>
								<el-empty :image-size="80" description="空数据" v-else></el-empty>
							</el-col>
						</el-row>
					</el-form>
				</el-tab-pane>

				</el-tabs>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="cancel">取 消</el-button>
					<el-button type="primary" @click="submit">确 定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>
<style scoped>
:deep(.el-select),
:deep(.el-input-number) {
	width: 100%;
}
</style>
<script lang="ts" setup>
	import { onMounted, reactive, ref } from 'vue';
	import { getDictDataItem as di, getDictDataList as dl } from '/@/utils/dict-utils';
	import { ElMessage } from "element-plus";
	import type { FormRules } from "element-plus";
	import { addBoardDisplayDesign, updateBoardDisplayDesign, detailBoardDisplayDesign ,getDetailBoardDisplayDesignDetail} from "/@/api/board/boardDisplayDesign";

	//父级传递来的参数
	var props = defineProps({
		title: {
		type: String,
		default: "",
	},
	});
	//父级传递来的函数，用于回调
	const emit = defineEmits(["reloadTable"]);
	const ruleFormRef = ref();
	const isShowDialog = ref(false);
	const ruleForm = ref<any>({});
	//自行添加其他规则
	const rules = ref<FormRules>({
		designNo: [{required: true, message: '请输入设计编号！', trigger: 'blur',},],
		name: [{required: true, message: '请输入组态名称！', trigger: 'blur',},],
	});
	const predefineColors = ref([
  '#ff4500',
  '#ff8c00',
  '#ffd700',
  '#90ee90',
  '#00ced1',
  '#1e90ff',
  '#c71585',
  'rgba(255, 69, 0, 0.68)',
  'rgb(255, 120, 0)',
  'hsv(51, 100, 98)',
  'hsva(120, 40, 94, 0.5)',
  'hsl(181, 100%, 37%)',
  'hsla(209, 100%, 56%, 0.73)',
  '#c7158577',
])
	const state = reactive({
		loading:false,
		selectedTabName:'0',
		DisplayForm:{
		Displayitems:ref<any>([])
		}

	})
	// 打开弹窗
	const openDialog = async (row: any) => {
		var items=[
         {
           itemType:'rect',
           top:10,
           left:10,
           width:20,
           height:40,
           color:'orange'
         },
         {
           itemType:'line',
           top:30,
           left:30,
           endTop:30,
           endLeft:50,
           width:1,
           color:'blue'
         },
         {
          sid:1,
           itemType:'switch',
           top:30,
           left:50,
           direction:1,
           state:1,
           arrow:true
         }
       ,
         {
           itemType:'rect',
           top:10,
           left:70,
           width:20,
           height:40,
           color:'orange'
         },
         ,
         {
          itemType:'line',
           top:50,
           left:80,
           endTop:70,
           endLeft:80,
           width:1,
           color:'blue'
         },
         {
          SVGAnimatedIntegerid:2,
           itemType:'switch',
           top:70,
           left:80,
           direction:2,
           state:0,
         
         },
         {
           itemType:'rect',
           top:120,
           left:60,
           width:40,
           height:20,
           color:'orange'
         },{
           itemType:'line',
           top:130,
           left:100,
           endTop:130,
           endLeft:150,
           width:1,
           color:'blue',
           arrow:false
         },
         
         {
           itemType:'line',
           top:130,
           left:150,
           endTop:70,
           endLeft:150,
           width:1,
           color:'blue',
           arrow:true
         },
         {
          sid:5,
           itemType:'switch',
           top:70,
           left:150,
           direction:4,
           state:0,
         
         },
         {
           itemType:'line',
           top:70,
           left:80,
           endTop:100,
           endLeft:80,
           width:1,
           color:'blue',
           arrow:true
         },
         {
          itemType:'line',
           top:30,
           left:90,
           endTop:30,
           endLeft:110,
           width:1,
           color:'blue'
         },
         {
          sid:3,
           itemType:'switch',
           top:30,
           left:110,
           direction:1,
           state:0,
           arrow:true
         },{
           itemType:'rect',
           top:10,
           left:130,
           width:50,
           height:40,
           color:'orange'
         },
       ];
		// ruleForm.value = JSON.parse(JSON.stringify(row));
		// 改用detail获取最新数据来编辑
		let rowData = JSON.parse(JSON.stringify(row));
		if (rowData.id){
			ruleForm.value = (await detailBoardDisplayDesign(rowData.id)).data.result;
			state.DisplayForm.Displayitems=(await getDetailBoardDisplayDesignDetail(rowData.id)).data.result;
		}
		else{
			ruleForm.value = rowData;
			for(var nn=0;nn<items.length;nn++){
				var item=items[nn];
				addMaterialRow();
				for (var key in item) {
					if (item.hasOwnProperty(key)) {
						state.DisplayForm.Displayitems[state.DisplayForm.Displayitems.length-1][key]=item[key];
					}
				}
				
			}
			//console.log(state.DisplayForm.Displayitems);
			//state.DisplayForm.Displayitems=items;
		}
		isShowDialog.value = true;
	};

	// 关闭弹窗
	const closeDialog = () => {
		emit("reloadTable");
		isShowDialog.value = false;
	};

	// 取消
	const cancel = () => {
		isShowDialog.value = false;
	};

	// 提交
	const submit = async () => {
		ruleFormRef.value.validate(async (isValid: boolean, fields?: any) => {
			if (isValid) {
				let values = ruleForm.value;
				values.extDetail=state.DisplayForm.Displayitems;
				if (ruleForm.value.id == undefined || ruleForm.value.id == null || ruleForm.value.id == "" || ruleForm.value.id == 0) {
					await addBoardDisplayDesign(values);
				} else {
					await updateBoardDisplayDesign(values);
				}
				closeDialog();
			} else {
				ElMessage({
					message: `表单有${Object.keys(fields).length}处验证失败，请修改后再提交`,
					type: "error",
				});
			}
		});
	};

// 增加路径
const addMaterialRow = () => {
	if (state.DisplayForm.Displayitems == undefined) state.DisplayForm.Displayitems = [];
	state.DisplayForm.Displayitems?.push({
		sid:null,
		itemType:'',
		top:0,
		left:0,
		endTop:null,
		endLeft:null,
		width:null,
		height:null,
		radius:null,
		direction:null,
		color:'',
		arrow:false

	});
};

// 删除路径
const deleteMaterialRow = (k: number) => {
	state.DisplayForm.Displayitems?.splice(k, 1);
};





	// 页面加载时
	onMounted(async () => {
	});

	//将属性或者函数暴露给父组件
	defineExpose({ openDialog });
</script>
<style >
</style>



